package com.equis.architecture.preloader {
	import com.equis.architecture.service.AbstractBusinessService;
	
	import mx.rpc.events.FaultEvent;
	
	public class ServiceBatchItem extends AbstractBatchItem {
		
		protected var service:AbstractBusinessService;
		protected var callMethod:Function;
		protected var callback:Function;
		
		public function ServiceBatchItem(_service:AbstractBusinessService, _callMethod:Function, _callback:Function, _requires:Array = null, ... _params) {
			super(_requires);
			this.params = _params;
			this.service = _service;
			this.callMethod = _callMethod;
			this.callback = _callback;
		}

		public override function call(_preCallback:Function):void {
			service.callbackFunction = onEvent;
			service.errorCallbackFunction = onError;
			super.call(_preCallback);
			callMethod.apply(service, params);
		}

		private function onEvent(result:*):void {
			callback(result);
			preCallback();
			complete = true;
		}
		
		private function onError(errorEvent:FaultEvent):void {
			preCallback(errorEvent.fault.faultString);
		}
		
		public override function toString():String {
			return "Service: " + service.serviceName + "; Method: " + callMethod;
		}
	}
}